package org.xilin

class TeacherController {
    def authenticateService
    def semesterService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list")
    }

    def list = {
        //params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def teacherList = semesterService.getListBySemester(Teacher)
        render(template: "list", model: [teacherInstanceList: teacherList, teacherInstanceTotal: teacherList.count()])
    }

    def create = {
        def teacherInstance = new Teacher()
        teacherInstance.properties = params
        render(template: "create", model: [teacherInstance: teacherInstance])
    }

    def save = {
        def teacherInstance = null

        if (params.id) {
            teacherInstance = Teacher.get(params.id)
            teacherInstance.user.properties = params
        }
        else {
            teacherInstance = new Teacher(params)
            teacherInstance.user.passwd = authenticateService.passwordEncoder(teacherInstance.user.passwd)
            teacherInstance.user.enabled = true
            teacherInstance.user.emailShow = true
            teacherInstance.user.description = 'teacher'
        }

        if (!teacherInstance.validate() || !teacherInstance.user.validate()) {
            teacherInstance.errors.allErrors.each {
                log.debug(it)
            }
            teacherInstance.user.errors.allErrors.each {
                log.debug(it)
            }
            flash.message = "Missing required data for Teacher"
            render(template: params.view, model: [teacherInstance: teacherInstance, userInstance: teacherInstance.user])
            return
        }

        def role = Role.findByAuthority("ROLE_TEACHER")
        role.addToPeople(teacherInstance.user)
        role.save(flush:true)

        if (teacherInstance.validate()) {
            teacherInstance.save(flush:true)
            teacherInstance.refresh()
            redirect(action: "list")
        }
        else {
            flash.message = "Missing required data for Teacher"
            render(template: params.view, model: [teacherInstance: teacherInstance])
        }
    }

    def show = {
        def teacherInstance = Teacher.get(params.id)
        if (!teacherInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
            redirect(action: "list")
        }
        else {
            [teacherInstance: teacherInstance]
        }
    }

    def edit = {
        def teacherInstance = Teacher.get(params.id)
        if (!teacherInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "edit", model: [teacherInstance: teacherInstance])
        }
    }

    def update = {
        def teacherInstance = Teacher.get(params.id)
        if (teacherInstance) {
            teacherInstance.properties = params
            if (!teacherInstance.hasErrors() && teacherInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'teacher.label', default: 'Teacher'), teacherInstance.id])}"
                redirect(action: "show", id: teacherInstance.id)
            }
            else {
                render(view: "edit", model: [teacherInstance: teacherInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def teacherInstance = Teacher.get(params.id)
        if (teacherInstance) {
            Course course = Course.findByTeacher(teacherInstance)

            if (course) {
                flash.message = "Cannot delet teacher '" + teacherInstance + "' it is referenced by course '" + course.name + "'."
            }
            else {
                try {
                    teacherInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
                }
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
        }
        redirect(action: "list")
    }
}
